package projectsys.sys.core.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import projectsys.sys.core.mapper.ModuleMapper;
import projectsys.sys.core.mapper.RoleMapper;
import projectsys.sys.core.mapper.RoleModuleMapper;
import projectsys.sys.core.mapper.UserRoleMapper;
import projectsys.sys.core.model.Module;
import projectsys.sys.core.model.ModuleExample;
import projectsys.sys.core.model.Role;
import projectsys.sys.core.model.RoleModule;
import projectsys.sys.core.model.RoleModuleExample;
import projectsys.sys.core.model.User;
import projectsys.sys.core.model.UserRole;
import projectsys.sys.core.model.UserRoleExample;
import projectsys.sys.core.service.ModuleManager;
import projectsys.sys.core.utils.ModuleTreeUtil;
import projectsys.sys.core.utils.cons.Constants;

@Service("ModuleManager")
public class ModuleManagerImpl implements ModuleManager{
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	RoleModuleMapper roleModuleMapper;
	@Autowired
	ModuleMapper moduleMapper;
	@Autowired
	RoleMapper roleMapper;
	
	@Override
	public String getMainMenuPro(String[] moduleCode, User user) {
		UserRoleExample ure = new UserRoleExample();
		ure.createCriteria().andUseridEqualTo(user.getId());
		List<UserRole> urList = this.userRoleMapper.selectByExample(ure);
		List<String> rmIdList = new ArrayList<String>(); // 所有角色id
		if (urList != null && urList.size() > 0) {
			for (UserRole o : urList) {
				rmIdList.add(o.getRoleid());
			}
		}
		// 获取用户包含的模块id
		RoleModuleExample rme = new RoleModuleExample();
		rme.createCriteria().andRoleidIn(rmIdList);
		List<RoleModule> rmlist = this.roleModuleMapper.selectByExample(rme);
		List<String> mIdList = new ArrayList<String>(); // 角色模块

		for (RoleModule o : rmlist) {
			mIdList.add(o.getModuleid());
		}
		
		// 根据站点code获取所有模块
		ModuleExample me = new ModuleExample();
		me.createCriteria().andEnabledEqualTo("01").andTypeEqualTo("01")
				.andCodeIn(Arrays.asList(moduleCode));
		
		List<Module> mList = this.moduleMapper.selectByExample(me); // 该代码下的模块
		List<String> moduleIdList = new ArrayList<String>(); // 角色模块
		for (RoleModule o : rmlist) {
			moduleIdList.add(o.getModuleid());
		}
		//获取登陆用户的所有角色
		List<Role> roles = roleMapper.selectRoleByUserId(user.getId());
		List<Module> list = ModuleTreeUtil.getInstance().getModulesByMid(roles, moduleIdList);
		// 获取该站点下的所有模块
		JSONArray modelJSON = new JSONArray();
		for(Module module : mList) { // 所有的模块
			modelJSON.add(listToTree(module.getId(), list));
		}
		return modelJSON.toJSONString();
	}
	
	
	public static JSONArray listToTree(String pid, List<Module> list) {
		JSONArray children = new JSONArray();
		for(int i = 0; i < list.size(); i++) {
			Module m = list.get(i);
			if("03".equals(m.getType())) { // 去掉权限
				continue;
			}
			
			if(pid.equals(m.getPid())) {
				JSONObject mj = (JSONObject) JSONObject.toJSON(m);
				mj.put("children", listToTree(m.getId(), list));
				children.add(mj);
			}
		}
		return children;
	}


	@Override
	public List<Module> getModuleTrees() {
		String id = Constants.SYS_MODULE_ROOT_ID;
		// 获得树及以下所有子树
		List<String> list=new ArrayList<String>();
		list.add("01");
		list.add("03");
		ModuleExample me = new ModuleExample();
		me.createCriteria().andPidEqualTo(id).andTypeEqualTo("01")
				.andEnabledIn(list);
		List<Module> modules = this.moduleMapper.selectByExample(me); // 查询出站点

		List<Module> moduleList = new ArrayList<Module>();

		for (Module module : modules) { // 查询处每个站点下的所有模块以及权限
			List<Module> childTrees = this.getAllChildTrees(module.getId());
			childTrees.add(module); // 该站点下所有模块及其权限 以及本身
			moduleList.addAll(childTrees);
		}
		return moduleList;
	}
	
	private List<Module> getAllChildTrees(String id) {
		List<String> list=new ArrayList<String>();
		list.add("01");
		list.add("03");
		List<Module> childTrees = new ArrayList<Module>();
		ModuleExample me = new ModuleExample();
		me.createCriteria().andPidEqualTo(id).andEnabledIn(list);
		me.setOrderByClause(" to_number(sortnumber)");
		List<Module> trees = this.moduleMapper.selectByExample(me);
		if (null != trees && trees.size() > 0) {
			childTrees.addAll(trees);
			for (Module tree : trees) {
				childTrees.addAll(getAllChildTrees(tree.getId()));
			}
		}
		return childTrees;
	}
	
}
